/*
* @Author: nccloud
* @PageInfo: 表头编辑后事件
*/
import { FIELDS, BUTTONID,URL,AREA,BILLTYPECODE } from '../../constance';
import { ajax ,cardCache, getBusinessInfo, toast} from 'nc-lightapp-front';
import { data } from 'jquery';
import { clearCardData, mainOrgInit } from '../utils/cardPageUtil';
import { byPkorgFiter, fiterFrkm } from '../../../../common/util/fiterUtils';
let {setDefData, getDefData } = cardCache;
 
export default function(props,moduleId, key) {

        if (key === FIELDS.pk_org) {
                props.resMetaAfterPkorgEdit(); // 选择主组织以后，恢复其他字段的编辑性
                props.button.setDisabled({
                                        [BUTTONID.cardBody_1_AddLine]: false,
                        [BUTTONID.cardBody_2_AddLine]: false
                });

                let value = props.form.getFormItemsValue(moduleId, 'pk_org'); 
                let pk_org = value.value;
                if(pk_org!=null && pk_org!=''){
                        filterByOrg(props, moduleId);
                }else{
                        clearCardData.call(this, props);  
                        let groupName = getBusinessInfo().groupName;
                        let groupId = getBusinessInfo().groupId;
                        let dbilldate = getBusinessInfo().businessDate;
                        props.form.setFormItemsValue(AREA.cardHead, { ['dbilldate']: { value: dbilldate } });
                        props.form.setFormItemsValue(AREA.cardHead, { ['pk_group']: { value: groupId ,display:groupName } }); 
                        props.form.setFormItemsValue(AREA.cardHead, { ['billtype']: { value: BILLTYPECODE } });
                }
              



        }
         if(key == "headacc"){

               
                if(props.form.getFormItemsValue(moduleId,"headacc") == ''){
                        props.form.setFormItemsRequired(AREA.cardHead, { 'cussup': false });
                        props.form.setFormItemsRequired(AREA.cardHead, { 'dept': false });
                        props.form.setFormItemsRequired(AREA.cardHead, { 'vdef4': false });
                        props.form.setFormItemsRequired(AREA.cardHead, { 'vdef2': false });
                        props.form.setFormItemsRequired(AREA.cardHead, { 'vdef3': false });
                        props.form.setFormItemsRequired(AREA.cardHead, { 'vdef1': false });
                } else{
                        ajax({
                                url:URL.QueryHeadacc,
                                data:props.form.getFormItemsValue(moduleId,"headacc") ,
                                success:(res)=>{
                                        let items = res.data;
                                        
                                        props.form.setFormItemsRequired(AREA.cardHead, { 'cussup': items.cussup });
                                        props.form.setFormItemsRequired(AREA.cardHead, { 'dept': items.dept });
                                        props.form.setFormItemsRequired(AREA.cardHead, { 'vdef4': items.vdef4 });
                                        props.form.setFormItemsRequired(AREA.cardHead, { 'vdef2': items.vdef2 });
                                        props.form.setFormItemsRequired(AREA.cardHead, { 'vdef3': items.vdef3 });
                                        props.form.setFormItemsRequired(AREA.cardHead, { 'vdef1': items.vdef1 });
                                }
                        })
                       
                }
               
               
        }
}

export function filterByOrg(props, moduleId){ 
        let value = props.form.getFormItemsValue(moduleId, 'pk_org'); 
        let pk_org = value.value;

        if(pk_org != null){

                ajax({
                        url: URL.queryAcc,
                        data: pk_org,
                        success: (res) => {
                                if(res.data.accbook!=null){
                                        setDefData("txaccbook","txaccbook",res.data.accbook);//表体科目过滤使用
                                }
                        }
                    });
                    byPkorgFiter(props, AREA.cardHead, 'cussup', pk_org, false);//客商
                    byPkorgFiter(props, AREA.cardHead, 'dept', pk_org, false);//部门
                    byPkorgFiter(props, AREA.cardBody_1, 'cussup', pk_org, false);//客商
        }

}